package com.wsc.wisdomchina.activitity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.wsc.wisdomchina.R;
import com.wsc.wisdomchina.Tools.JsonTools;
import com.wsc.wisdomchina.Tools.Tools;
import com.wsc.wisdomchina.activitity.baseactivity.BaseActivity;
import com.wsc.wisdomchina.callback.ContributorView;
import com.wsc.wisdomchina.constant.Constant;
import com.wsc.wisdomchina.javaBean.RespObj;
import com.wsc.wisdomchina.presenter.ContributorPresenter;
import com.wsc.wisdomchina.view.MyToast;


public class ChangeNickerNameActivity extends BaseActivity implements View.OnClickListener,ContributorView {
    private TextView submit_chang_nicker_name;
    private ImageView regist_back;
    private EditText nicker_name;
    private MyToast myToast;
    private Context context;
    private TextView tv_headtitle;
    private ContributorPresenter contributorPresenter;
    private final String MY_MODULE_UPDATE_NICKNAME_FLAG="MY_MODULE_UPDATE_NICKNAME_FLAG";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.change_nickname_layout);
        context=this;
        myToast=new MyToast(context);
        initView();
        String nickname=getIntent().getStringExtra("nickname");
        if(nickname!=null && !nickname.isEmpty())
        {
            nicker_name.setHint(nickname);
        }
        contributorPresenter = new ContributorPresenter(this, context);

    }

    private void initView() {
        submit_chang_nicker_name = (TextView) findViewById(R.id.tv_submit);
        regist_back = (ImageView) findViewById(R.id.regist_back);
        nicker_name = (EditText) findViewById(R.id.change_nicker_name);
        submit_chang_nicker_name.setText("提交");
        submit_chang_nicker_name.setOnClickListener(this);
        regist_back.setOnClickListener(this);
        tv_headtitle=(TextView) findViewById(R.id.tv_headtitle);
        tv_headtitle.setText("修改昵称");
    }
    @Override
    public void onLoadContributorStart(String flag) {

    }

    @Override
    public void onLoadContributorSuccess(String jsonString, String flag) {
        try {
            //发送验证码
            if (flag.equals(MY_MODULE_UPDATE_NICKNAME_FLAG)) {
                RespObj respObj = JsonTools.jsonStringToObject(jsonString, RespObj.class);
                String code=respObj.getCode();
                if(code.equals("10700"))
                {
                    myToast.showShort("操作失败");
                }else if(code.equals("200"))
                {
                    if(respObj.getMessage().equals("41"))
                    {
                        Intent intent=getIntent();
                        intent.putExtra("nickname",nicker_name.getText().toString().trim());
                        setResult(0,intent);
                        finish();
                    }
                }
            }

        } catch (Exception e) {

        }
    }

    @Override
    public void onLoadContributorComplete(String flag) {
    }

    @Override
    public void onLoadContributorError(String error,String flag) {

    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.regist_back:
                finish();
                break;
            case R.id.tv_submit:

                String nickNameString=nicker_name.getText().toString().trim();
                if(nickNameString!=null && !nickNameString.isEmpty())
                {
                    String uid= Tools.getSharedPreferencesStrings(Constant.SharePreferenceUserid,"",context);
                    contributorPresenter.getUpdateNickName(uid,nickNameString,MY_MODULE_UPDATE_NICKNAME_FLAG);
                }else
                {
                    myToast.showShort("昵称不能为空");
                }
                break;
        }
    }

}
